//
//  TrafficSignalTimer.m
//  Portable Traffic Signal Assistor
//
//  Created by Hao Xu on 4/5/12.
//  Copyright (c) 2012 TTU. All rights reserved.
//

#import "TrafficSignalTimer.h"

@implementation TrafficSignalTimer

+ (NSString *) getCurrentTimeString
{
    NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
    NSDate *currentDateTime = [NSDate date]; 
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setTimeZone:localTimeZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateInStringFormated = [dateFormatter stringFromDate:currentDateTime];
    NSLog(@"%@", dateInStringFormated);
//    NSDate *currentDateTime_Zone=[dateFormatter dateFromString:dateInStringFormated];
//    NSString *dateOnlyString;
    NSString *timeOnlyString=nil;
    NSRange range = [dateInStringFormated rangeOfString:@" "];
    if (range.location != NSNotFound) {
        
//        dateOnlyString = [dateInStringFormated substringToIndex:(range.location)];
        timeOnlyString=[dateInStringFormated substringFromIndex:(range.location)];
        
    }
    return timeOnlyString;
    
}
+ (NSString *) getMasterTimeString:(NSString *)standardTimeOffset
{
    NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
    NSDate *currentDateTime = [NSDate date]; 
    NSDate *agencyTime=[currentDateTime dateByAddingTimeInterval:[standardTimeOffset intValue]];
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setTimeZone:localTimeZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateInStringFormated = [dateFormatter stringFromDate:agencyTime];
    NSLog(@"%@", dateInStringFormated);
    //    NSDate *currentDateTime_Zone=[dateFormatter dateFromString:dateInStringFormated];
//    NSString *dateOnlyString;
    NSString *timeOnlyString=nil;
    NSRange range = [dateInStringFormated rangeOfString:@" "];
    if (range.location != NSNotFound) {
        
//        dateOnlyString = [dateInStringFormated substringToIndex:(range.location)];
        timeOnlyString=[dateInStringFormated substringFromIndex:(range.location)];
        
    }
    return timeOnlyString;
}
+ (int) calculateCycleTimer:(int) cycle time:(NSDate *)date offSet:(int)offSet syncTime:(NSString *)syncTime standardTimeOff:(int)standardTimeOff
{
    NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
//    NSDate *currentDateTime = [NSDate date];    
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setTimeZone:localTimeZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss aa"];
    [dateFormatter setDateFormat:@"HH"];
    int currentHour=[[dateFormatter stringFromDate:date] intValue];
    [dateFormatter setDateFormat:@"mm"];
    int currentMinute=[[dateFormatter stringFromDate:date] intValue];
    [dateFormatter setDateFormat:@"ss"];
    int currentSecond=[[dateFormatter stringFromDate:date] intValue];
    

    
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateOnlyString=[dateFormatter stringFromDate:date];
    NSString * initialTimeString=[[dateOnlyString stringByAppendingString:@" "] stringByAppendingString:syncTime];
    NSLog(@"%@", initialTimeString);
    
    NSDateFormatter *inputFormat = [[[NSDateFormatter alloc] init] autorelease];
    [inputFormat setTimeZone:localTimeZone];
    [inputFormat setDateFormat:@"yyyy-MM-dd hh:mm aa"];
    //    [inputFormat setDateStyle:NSdate
    NSDate *syncDate = [inputFormat dateFromString: initialTimeString];
    
    
    [dateFormatter setDateFormat:@"HH"];
    int syncHour=[[dateFormatter stringFromDate:syncDate] intValue];
    [dateFormatter setDateFormat:@"mm"];
    int syncMinute=[[dateFormatter stringFromDate:syncDate] intValue];
    [dateFormatter setDateFormat:@"ss"];
    int syncSecond=[[dateFormatter stringFromDate:syncDate] intValue];

    NSInteger secondsBetweenTimes = (currentHour-syncHour)*3600+(currentMinute-syncMinute)*60+(currentSecond-syncSecond);
    int cycleTimer=(secondsBetweenTimes+(cycle-offSet))%cycle;
    return cycleTimer;
    
}
+ (int) calculateCycleTimer:(int) cycle offSet:(int)offSet syncTime:(NSString *)syncTime standardTimeOff:(int)standardTimeOff
{
    NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
    NSDate *currentDateTime = [NSDate date]; 
//    NSDate *currentDateTime = [NSDate date]; 
    NSDate *agencyTime=[currentDateTime dateByAddingTimeInterval:standardTimeOff];
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setTimeZone:localTimeZone];
//    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss aa"];
    [dateFormatter setDateFormat:@"HH"];
//    int currentHour=[[dateFormatter stringFromDate:agencyTime] intValue];
//    [dateFormatter setDateFormat:@"mm"];
//    int currentMinute=[[dateFormatter stringFromDate:agencyTime] intValue];
//    [dateFormatter setDateFormat:@"ss"];
//    int currentSecond=[[dateFormatter stringFromDate:agencyTime] intValue];
//    NSString *dateInStringFormated = [dateFormatter stringFromDate:agencyTime];
//    NSLog(@"%@", dateInStringFormated);
//    NSDate *currentDateTime_Zone=[dateFormatter dateFromString:dateInStringFormated];
//    NSString *dateOnlyString;
//    NSRange range = [dateInStringFormated rangeOfString:@" "];
//    if (range.location != NSNotFound) {
//        
//        dateOnlyString = [dateInStringFormated substringToIndex:(range.location)];
//        
//    }else {
//        return 0;
//    }
//    
//    NSString *timerGroupSyncTime= syncTime;
//    
//    //    NSString *timerGroupSyncTimeWithSeconds = [[[timerGroupSyncTime substringToIndex:range.location] stringByAppendingString:@":00"] stringByAppendingString:[timerGroupSyncTime substringFromIndex:range.location]];
//    [dateInStringFormated substringToIndex:(range.location)];
//    
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateOnlyString=[dateFormatter stringFromDate:agencyTime];
    NSString * initialTimeString=[[dateOnlyString stringByAppendingString:@" "] stringByAppendingString:syncTime];
    NSLog(@"%@", initialTimeString);
    
    NSDateFormatter *inputFormat = [[[NSDateFormatter alloc] init] autorelease];
    [inputFormat setTimeZone:localTimeZone];
    [inputFormat setDateFormat:@"yyyy-MM-dd hh:mm aa"];
    //    [inputFormat setDateStyle:NSdate
    NSDate *syncDate = [inputFormat dateFromString: initialTimeString];
    
    if ([agencyTime compare:syncDate]==NSOrderedAscending) {
         NSLog(@"date1 is earlier than date2");
        syncDate=[syncDate dateByAddingTimeInterval:(-3600*24)];
    }
    
    /*
     if ([date1 compare:date2] == NSOrderedDescending) {
     NSLog(@"date1 is later than date2");
     
     } else if ([date1 compare:date2] == NSOrderedAscending) {
     NSLog(@"date1 is earlier than date2");
     
     } else {
     NSLog(@"dates are the same");
     
     }
     */
    
   
//    [dateFormatter setDateFormat:@"HH"];
//    int syncHour=[[dateFormatter stringFromDate:syncDate] intValue];
//    [dateFormatter setDateFormat:@"mm"];
//    int syncMinute=[[dateFormatter stringFromDate:syncDate] intValue];
//    [dateFormatter setDateFormat:@"ss"];
//    int syncSecond=[[dateFormatter stringFromDate:syncDate] intValue];
    
    // Release the dateFormatter
    
//    [dateFormatter release]; 
    
//    NSTimeInterval distanceBetweenTimes = [currentDateTime_Zone timeIntervalSinceDate:syncDate];
    //    double secondsInAnHour = 3600;
//    NSInteger secondsBetweenTimes = (currentHour-syncHour)*3600+(currentMinute-syncMinute)*60+(currentSecond-syncSecond);
    NSInteger secondsBetweenTimes =[agencyTime timeIntervalSinceDate:syncDate];
    if (cycle==0) {
        return 0;
    }
    int cycleTimer=(secondsBetweenTimes+(cycle-offSet))%cycle;
    if (cycleTimer==0) {
        cycleTimer=cycle;
    }
    return cycleTimer;

}

+ (int) calculateCycleTimer:(NSString *)timerGroupID signalID:(NSString *) signalID
{
    TimerGroup *timerGroup=[XMLParser getTimerGroup:timerGroupID];
    Signal *signal=[XMLParser getSignalByTimerGroupIDAndSignalID:timerGroupID signalID:signalID];
    // Get current date time
    NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
    NSDate *currentDateTime = [NSDate date];
    
    
    // Instantiate a NSDateFormatter
    
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setTimeZone:localTimeZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss aa"];
    
    NSString *dateInStringFormated = [dateFormatter stringFromDate:currentDateTime];
    NSLog(@"%@", dateInStringFormated);
    NSDate *currentDateTime_Zone=[dateFormatter dateFromString:dateInStringFormated];
    NSString *dateOnlyString;
    NSRange range = [dateInStringFormated rangeOfString:@" "];
    if (range.location != NSNotFound) {
        
        dateOnlyString = [dateInStringFormated substringToIndex:(range.location)];
        
    }else {
        return 0;
    }
    
    NSString *timerGroupSyncTime= timerGroup.syncTime;
    
    //    NSString *timerGroupSyncTimeWithSeconds = [[[timerGroupSyncTime substringToIndex:range.location] stringByAppendingString:@":00"] stringByAppendingString:[timerGroupSyncTime substringFromIndex:range.location]];
    [dateInStringFormated substringToIndex:(range.location)];
    
    NSString * initialTimeString=[[dateOnlyString stringByAppendingString:@" "] stringByAppendingString:timerGroupSyncTime];
    NSLog(@"%@", initialTimeString);
    
    NSDateFormatter *inputFormat = [[[NSDateFormatter alloc] init] autorelease];
    [inputFormat setTimeZone:localTimeZone];
    [inputFormat setDateFormat:@"yyyy-MM-dd HH:mm aa"];
    //    [inputFormat setDateStyle:NSdate
    NSDate *syncDate = [inputFormat dateFromString: initialTimeString];
    
    
    // Release the dateFormatter
    
//    [dateFormatter release]; 
    
    NSTimeInterval distanceBetweenTimes = [currentDateTime_Zone timeIntervalSinceDate:syncDate];
    //    double secondsInAnHour = 3600;
    NSInteger secondsBetweenDates = distanceBetweenTimes;
    int cycleTimer=(secondsBetweenDates+([signal.cycleLength intValue]-[signal.offset intValue]))%[signal.cycleLength intValue];
    return cycleTimer;

}
@end
